package com.njtoyo.taxi.admin.excel;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.njtoyo.taxi.admin.mapper.master.entity.Company;
import com.njtoyo.taxi.admin.mapper.master.entity.Driver;
import com.njtoyo.taxi.admin.mapper.master.entity.DriverDeviceRelationship;
import com.njtoyo.taxi.admin.mapper.master.entity.Taxi;
import com.njtoyo.taxi.admin.mapper.master.mapper.CompanyMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.DriverDeviceRelationshipMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.DriverMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.TaxiMapper;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author mdf
 * @title: 导出示例
 * @description: TODO
 * @date 2024/4/11 15:15
 */
@Slf4j
@SpringBootTest
@ActiveProfiles(value = "prod")
public class 导出示例 {

    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private DriverMapper driverMapper;
    @Autowired
    private TaxiMapper taxiMapper;
    @Autowired
    private DriverDeviceRelationshipMapper driverDeviceRelationshipMapper;


    @Test
    public void export() {

        List<Company> companyList = companyMapper.selectList(null);
        for (Company company : companyList) {
            List<Taxi> taxiList = taxiMapper.selectList(Wrappers.<Taxi>lambdaQuery().eq(Taxi::getCompanyId, company.getId()));
            if (CollUtil.isNotEmpty(taxiList)) {
                List<Long> taxiIds = taxiList.stream().map(Taxi::getId).collect(Collectors.toList());
                List<Driver> drivers = driverMapper.selectList(Wrappers.<Driver>lambdaQuery().isNotNull(Driver::getDeviceInfo).in(Driver::getTaxiId, taxiIds));
                List<Map<String, Object>> rows = new ArrayList<>();
                for (Driver driver : drivers) {
                    List<DriverDeviceRelationship> driverDeviceRelationships = driverDeviceRelationshipMapper.selectList(Wrappers.<DriverDeviceRelationship>lambdaQuery()
                            .eq(DriverDeviceRelationship::getState, 1)
                            .eq(DriverDeviceRelationship::getDriverId, driver.getId()));
                    Map<String, Object> row = new LinkedHashMap<>();
                    row.put("司机名称", driver.getName());
                    row.put("司机手机号", driver.getMobileNumber());
                    row.put("最后登录时间", CollUtil.isNotEmpty(driverDeviceRelationships) ? driverDeviceRelationships.get(0).getUpdatedAt() : null);
                    row.put("最后登录设备", StrUtil.isNotBlank(driver.getDeviceInfo()) ? JSON.parseObject(driver.getDeviceInfo()).getString("d") : "");
                    rows.add(row);
                }
                if (CollUtil.isNotEmpty(rows)) {
                    ExcelWriter writer = ExcelUtil.getWriter("/Users/saber/Desktop/company/" + company.getName() + ".xlsx");
                    writer.write(rows, true);
                    writer.close();
                }
            }


        }

    }
}
